package bingo.rpc.framework.cluster.engine;

import bingo.rpc.framework.cluster.ClusterStrategy;
import bingo.rpc.framework.cluster.impl.*;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @author hujinsheng
 * @project bingo
 * @create 2017-12-14 13:36
 **/
public class ClusterEngine {
    private static final Map<ClusterStrategyEnum,ClusterStrategy> clusterStrategyMap= Maps.newConcurrentMap();
    static {
        clusterStrategyMap.put(ClusterStrategyEnum.Random,new RandomClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.WeightRandom,new WeightRandomClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.RoundRobin,new RoundRobinClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.WeightRoundRobin,new WeightRandomClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.Hash,new HashClusterStrategyImpl());
    }
    public static ClusterStrategy queryClusterStragety(String clusterStragety){
        ClusterStrategyEnum clusterStrategyEnum = ClusterStrategyEnum.queryByCode(clusterStragety);
        if(clusterStrategyEnum==null){
            //选择默认的随机算法
            return new RandomClusterStrategyImpl();
        }
        return clusterStrategyMap.get(clusterStrategyEnum);


    }
}
